package com.hqully.smartfumehood.controller;


import com.github.pagehelper.PageInfo;
import com.hqully.smartfumehood.common.utils.PermissionUtil;
import com.hqully.smartfumehood.common.utils.ResultVoUtil;
import com.hqully.smartfumehood.common.vo.QueryVo;
import com.hqully.smartfumehood.common.vo.ResultVo;
import com.hqully.smartfumehood.entity.Laboratory;
import com.hqully.smartfumehood.entity.dto.LaboratoryDto;
import com.hqully.smartfumehood.service.LaboratoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lly
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/api/laboratory")
public class LaboratoryController extends BaseController{
    @Autowired
    private LaboratoryService laboratoryService;

    @PostMapping("/find-all-laboratory")
    public ResultVo findAllLaboratory(@RequestHeader("token") String token,@RequestBody QueryVo queryVo){

        QueryVo realQueryVo = PermissionUtil.setQueryUnitId(token, queryVo);

        PageInfo<LaboratoryDto> allLaboratory = laboratoryService.findAllLaboratory(realQueryVo);


        return ResultVoUtil.success(allLaboratory);
    }

    @PostMapping("/add-laboratory")
    public ResultVo addLaboratory(@RequestBody Laboratory laboratory){

        try {
            int status = laboratoryService.addLaboratory(laboratory);
        } catch (DuplicateKeyException e) {
            return ResultVoUtil.error("该实验室编号已存在");
        }


        return ResultVoUtil.success("添加实验室成功");
    }

    @PostMapping("/delete-laboratory")
    public ResultVo deleteLaboratory(@RequestBody Laboratory laboratory){
        laboratoryService.deleteLaboratory(laboratory.getId());

        return ResultVoUtil.success("删除实验室成功");
    }

    @PostMapping("/update-laboratory")
    public ResultVo updateLaboratory(@RequestBody Laboratory laboratory){
        laboratoryService.updateLaboratory(laboratory);

        return ResultVoUtil.success("更新实验室成功");
    }

}
